/*
 * @Author: wwssaabb
 * @Date: 2021-08-18 15:20:38
 * @LastEditTime: 2021-11-18 11:20:59
 * @FilePath: \hospitable-rental-for-react\src\api\ajax.js
 */

import axios from 'axios'
import {
  Toast
} from 'antd-mobile'

// export const BASEURL = 'http://localhost:8080'
export const BASEURL = 'http://8.129.105.10:8890'
axios.defaults.baseURL = BASEURL

export default function ajax(url, data, method = 'GET', headers) {
  return new Promise((resolve) => {
    let promise

    if (method === 'GET') {
      promise = axios.get(url, {
        params: data,
        headers: {
          'Cache-Control': 'max-age=300'
        }
      })
    } else {
      promise = axios({
        url,
        data,
        method: method,
        headers: {
          ...headers
        }
      })
    }

    promise
      .then(res => resolve(res.data))
      .catch(err => Toast.fail(err.message))
  })
}